// 轮次
class Round {

    /**
     * 
     * @param {number} roll1 第一投命中数
     * @param {number} roll2 第二投命中数
     */
    constructor(roll1, roll2) {
        this.roll1 = roll1;
        if (roll2) {
            this.roll2 = roll2;
        } else {
            this.roll2 = '-';
        }
        this.appendScore = 0;
    }

    /**
     * 获取得分
     * @returns 得分
     */
    getScore() {
        const score1 = this.getRoll1Score();
        if (score1 == 30) {
            return 30;
        }
        const score2 = this.getRoll2Score();
        if (score2 + score1 == 10) {
            return 10 + this.appendScore;
        }
        return score1 + score2;
    }

    getShow() {
        const score = this.getScore();

        if (score == 30) {
            return '30';
        }
        let str = this.getRoll1Score();
        const score2 = this.getRoll2Score();
        let flag = false;
        if (score2 != 0) {
            str += `+${score2}`;
            flag = true;
        }
        if (this.appendScore != 0) {
            str += `+${this.appendScore}`;
            flag = true;
        }
        if (flag) {
            return "(" + str + ")";
        }
        return str;
    }


    getRoll1Score() {
        if (this.roll1 == 'X') {
            return 30
        }
        if (this.roll1 == '-') {
            return 0;
        }
        return Number(this.roll1);
    }

    getRoll2Score() {
        if (this.roll2 == '-') {
            return 0;
        } else if (this.roll2 == '/') {
            return 10 - this.getRoll1Score();
            // 奖励球
        } else if (this.roll2 == 'X') {
            return 30;
        } else {
            return Number(this.roll2);
        }
    }

    /**
     * 当补中之后，加上的分
     * @param {number} score 
     */
    append(appendScore) {
        if (this.roll2 != '/') {
            return;
        }
        if (appendScore == 'X') {
            this.appendScore = 10;
        } else if (appendScore == '-') {
            this.appendScore = 0
        } else {
            this.appendScore = Number(appendScore);
        }

    }
}

/**
 * 分析字符串获取Round对象
 * @param {string} param 
 * @returns 
 */
function getRound(param) {
    if (param == 'X') {
        return new Round('X', '-');
    }
    return new Round(param[0], param[1])
}

/**
 * 获取总分
 * @param {Array} arr 
 */
function getAllScore(arr) {
    // 总分
    let sum = 0;

    // 先循环一次
    for (let i = 0; i < arr.length; i++) {
        if (i >= 1) {
            arr[i - 1].append(arr[i].roll1);
        }
    }

    let roundStr = "          ";
    let roll1Str = "第一次投球 ";
    let roll2Str = "第二次投球 ";
    let rolleShow = "每轮得分数 "
    for (let i = 0; i < arr.length; i++) {
        if (i < 10) {
            roundStr += `${i}轮\t`;
        } else {
            roundStr += `奖励球`;
        }

        roll1Str += arr[i].roll1 + "\t";
        roll2Str += arr[i].roll2 + "\t";
        rolleShow += arr[i].getShow() + "\t";
        if (i <= 9) {
            sum += arr[i].getScore();
        }
    }
    console.log(roundStr);
    console.log(roll1Str);
    console.log(roll2Str);
    console.log(rolleShow);
    console.log("总得分：", sum)
}

// 测试案例
let arr = ['X', '3/', '3-', '9-', '17', '2-', '27', 'X', '2/', 'X', '3'];

getAllScore(arr.map(v => getRound(v)))